﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Db4objects.Db4o;
using Db4objects.Db4o.Query;
using Db4objects.Db4o.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for PhotoManager.xaml
	/// </summary>
	public partial class PhotoManager : Window
	{
		Invoice inv;
		String filename = "";
		IEnumerable<Photo> photoList;
		//Database dbase = Database.Instance;
        IObjectContainer db = Db4oFactory.OpenFile("img_bin.db4o");
		public PhotoManager(Invoice profile)
		{
			this.InitializeComponent();
			// Insert code required on object creation below this point.
			inv = profile;
			populate();
			
		}
		
		private void populate()
		{
            path.Text = "";
            filename = "";
			photoList = from Photo p in db
						where p.invoice == inv.inv_num
						select p;
			if (photoList != null)
			{
				foreach(Photo pic in photoList)
				{
                    Image tmp = new Image();
                    tmp.Source = deserialize(pic.image);
					grid.Children.Add(tmp);
				}
			}
		}

		private void dirClick(object sender, System.Windows.RoutedEventArgs e)
		{
			
			Microsoft.Win32.OpenFileDialog opnfl = new Microsoft.Win32.OpenFileDialog();
			opnfl.FileName = "Image";
			opnfl.DefaultExt = ".jpg";
			opnfl.Filter = "Picture files (.jpg)|*.jpg";
			
			if(opnfl.ShowDialog() == true)
			{
				filename = opnfl.FileName;
				path.Text = filename;
			}
			
		}

		private void uploadClick(object sender, System.Windows.RoutedEventArgs e)
		{
			if(filename != "")
			{
                
				// create image objects
				BitmapImage bi = new BitmapImage(new Uri(filename));
                Photo custPhoto = new Photo();

                // set invoice num, convert to blob,  and store to db
                custPhoto.invoice = inv.inv_num;
                custPhoto.image = serialize(bi);
				db.Store(custPhoto);
				db.Commit();
                // add pic to viewer
                Image pic = new Image();
                pic.Source = bi;
                grid.Children.Add(pic);	
			}
			else
				MessageBox.Show("Must choose a file to upload.");
			
		
		}

        private byte[] serialize(BitmapImage p)
        {
            MemoryStream memStream = new MemoryStream();
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(p));
            encoder.Save(memStream);
            return memStream.GetBuffer();      
        }

        private BitmapImage deserialize(byte[] array)
        {
            MemoryStream memStream = new MemoryStream(array);
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.StreamSource = memStream;
            img.EndInit();
            return (img);        
        }

        private void atexit(object sender, System.ComponentModel.CancelEventArgs e)
        {
			db.Close();
        }

        private void onMouseClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
			if(e.ClickCount == 2)
			{
				if(e.Source is Image)
				{


					photoViewer pv = new photoViewer(e.Source as Image);
                    pv.Topmost = true;
					pv.Show();
				}
				
			}
        }

       
	}
}